package com.jp.knowledge.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.logic.TopicLogic;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.TopicInfo;

/**
 * Created by 528787067@qq.com on 2017/1/7.
 */

public class TalkBottomTopicView extends TalkBottomView {

    private static final int PRAISE_REQUEST_CODE = 0x05;

    private TextView praiseText;
    private ImageView praiseIcon;
    private ImageView payRewardIcon;
    private TopicInfo topicInfo;

    private int praiseNum;
    private int payRewardNum;

    public TalkBottomTopicView(Context context) {
        this(context, null);
    }
    public TalkBottomTopicView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public TalkBottomTopicView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        praiseNum = 0;
        payRewardNum = 0;

        findViewById(R.id.comment_view).setVisibility(View.GONE);
        findViewById(R.id.icon_sc).setVisibility(View.GONE);
        findViewById(R.id.blq_view).setVisibility(View.GONE);
        findViewById(R.id.send_remind_tv).setVisibility(View.GONE);
        findViewById(R.id.topic_view).setVisibility(View.VISIBLE);

        praiseText = (TextView) contentView.findViewById(R.id.text_praise);
        praiseIcon = (ImageView) contentView.findViewById(R.id.icon_praise);
        payRewardIcon = (ImageView) contentView.findViewById(R.id.icon_pay_reward);

        praiseIcon.setOnClickListener(this);
        payRewardIcon.setOnClickListener(this);

        sendBtn.setTextColor(getResources().getColor(R.color.font_black_think));
//        edit.setHint("话题等待你来畅谈...");
        edit.removeTextChangedListener(textWatcher);

        topicLogic.setTopicCallBack(new TopicLogic.TopicDataCallBack() {
            @Override
            public void done(TopicInfo topic) {

            }
        });
    }

    @Override
    protected boolean checkTextEnable(String content){
        if(content == null || content.trim().length() == 0){
            ToasUtil.toast(mContext, "请输入有效的内容后再评论");
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.icon_praise:
                doPraise();
                return;
            case R.id.icon_pay_reward:
                doPayreward();
                return;
        }
        super.onClick(view);
    }

    public void setTopicInfo(TopicInfo topicInfo){
        this.topicInfo = topicInfo;
    }

    public void setPraiseNum(int num){
        this.praiseNum = num;
        praiseText.setText(this.praiseNum + "");
    }
    public void setPayRewardNum(int num){
        this.payRewardNum = num;
    }

    /* 点赞 */
    public void doPraise(){
        if(topicInfo == null){
            if(actionCallback != null)
                actionCallback.praiseCallback(false);
            return;
        }
        JsonObject param = new JsonObject();
        param.addProperty("infoId", infoId);
        param.addProperty("topicId", topicInfo.getTopicId());
        param.addProperty("commentId", topicInfo.getCommentId());
        HttpHelper.getInstance(mContext).topicPraise(param, PRAISE_REQUEST_CODE, this);
    }

    /* 打赏 */
    public void doPayreward(){

    }

    private ActionCallback actionCallback;
    public void setActionCallback(ActionCallback callback){
        this.actionCallback = callback;
    }
    /* 点赞或打赏后的回调 */
    public interface ActionCallback{
        void praiseCallback(boolean isSuccess);      // 点赞回调
        void payCewardBallback(boolean isSuccess);   // 打赏回调
    }

    @Override
    public void onNext(IModel data, int requestType) {
        if(requestType == PRAISE_REQUEST_CODE && actionCallback != null){
            int code = data.getErrcode();
            actionCallback.praiseCallback(code == 0);
            return;
        }
        super.onNext(data, requestType);
    }

    @Override
    public void onError(int requestType) {
        super.onError(requestType);
        if(requestType == PRAISE_REQUEST_CODE && actionCallback != null){
            actionCallback.praiseCallback(false);
        }
    }
}
